import { ESCesiumViewer } from "@czmSrc/ESCesiumViewer";
import { ESJVector3D, geoDestination, geoDistance, geoHeading } from "earthsdk3";
import { extendClassProps, react, reactArray, reactArrayWithUndefined, ReactivePropsToNativePropsAndChanged } from "xbsj-base";
import { MultiPointsModifyEditing } from ".";

export class DoublePointsModifyEditing extends MultiPointsModifyEditing {
    public init() {
        // 初始化
        const origin = this.startPosition;
        if (!origin) {
            return;
        }
        const distance = this.distance;
        const rotation = this.rotation;
        const groundDistance = distance * Math.cos(rotation[1] * Math.PI / 180);
        const des = geoDestination(origin, groundDistance, rotation[0]);
        if (!des) {
            this.positions = [origin];
            return;
        }
        const height = distance * Math.sin(rotation[1] * Math.PI / 180);
        des[2] += height;
        this.positions = [origin, des];
        if (this.editingPoints.length == 2) {
            this.editingPoints.get(0).position = origin;
            this.editingPoints.get(1).position = des;
        }
    }
    constructor(czmViewer: ESCesiumViewer) {
        super(czmViewer);
        this.loop = true; //默认不闭合
        this.maxPointsNum = 2;
        this.middlePointShow = false;
        this.moveWithFirstPosition = true;
        {
            const update = () => {
                do {
                    if (!this.positions || this.positions.length == 0) {
                        this.distance = 1;
                        this.rotation = [90, 0, 0];
                        this.startPosition = [0, 0, 0];
                        break;
                    }
                    this.startPosition = this.positions[0];
                    if (this.positions.length == 1) {
                        this.editingPoints.get(0).menuPoi.commands = [
                            ['add', '增加控制点', '', MultiPointsModifyEditing.baseImageUrl + 'add.png'],
                            ['delete', '删除当前控制点', '', MultiPointsModifyEditing.baseImageUrl + 'delete.png'],
                            ['modify', '修改当前控制点', '', MultiPointsModifyEditing.baseImageUrl + 'modify.png'],
                        ];
                        this.rotation = [90, 0, 0];
                        this.distance = 1;
                        break;
                    }
                    this.endPosition = this.positions[1];
                    for (let e of this.editingPoints) {
                        e.menuPoi.commands = [
                            ['delete', '删除当前控制点', '', MultiPointsModifyEditing.baseImageUrl + 'delete.png'],
                            ['modify', '修改当前控制点', '', MultiPointsModifyEditing.baseImageUrl + 'modify.png'],
                        ];
                    }
                    const heading = geoHeading(this.startPosition, this.endPosition);
                    const groundDistance = geoDistance(this.startPosition, this.endPosition);
                    if (Math.abs(groundDistance) <= 0) {
                        this.rotation = [90, 0, 0];
                        this.distance = 1;
                        return;
                    }
                    const height = this.endPosition[2] - this.startPosition[2];
                    const pitch = Math.atan(height / groundDistance) * 180 / Math.PI;
                    const distance = Math.sqrt(groundDistance * groundDistance + height * height);
                    this.rotation = [heading, pitch, 0];
                    this.distance = distance;
                } while (false);
            }
            // update();
            this.ad(this.positionsChanged.don(update));
        }
    }
}
export namespace DoublePointsModifyEditing {
    export const createDefaultProps = () => ({
        ...MultiPointsModifyEditing.createDefaultProps(),
        startPosition: reactArrayWithUndefined<ESJVector3D>(undefined),
        endPosition: reactArrayWithUndefined<ESJVector3D>(undefined),
        rotation: reactArray<ESJVector3D>([0, 0, 0]),
        distance: react(0),
    })
}
extendClassProps(DoublePointsModifyEditing.prototype, DoublePointsModifyEditing.createDefaultProps);
export interface DoublePointsModifyEditing extends ReactivePropsToNativePropsAndChanged<ReturnType<typeof DoublePointsModifyEditing.createDefaultProps>> { }